package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class UploaddedFileData
{
	private static final String EXP = "\\.[a-zA-Z]+"; 
	private InputStream inStream;
	private String extension = null;
	
	public UploaddedFileData(InputStream in, String filename)
	{
		//initialize Pattern
		Pattern p = Pattern.compile(EXP);
		Matcher m = p.matcher(filename);
		int i;
		if(m.find())
		{
			i = m.start();
			//j = m.end();
			extension = filename.substring(i);
		}
		
		inStream = in;
	}
	
	public void writeFile(File file) throws IOException
	{
		OutputStream out = new FileOutputStream(file);
		
		int len;
		byte buff[] = new byte[256];
		while((len=inStream.read(buff))>0)
			out.write(buff, 0, len);
		
		out.flush();
		out.close();
	}

	public InputStream getInStream() {
		return inStream;
	}

	public void setInStream(InputStream inStream) {
		this.inStream = inStream;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}
}